<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<script>

    /*验证代码是否符合规则*/
    function validRule(inputId, inputId2) {
        var parent = $.trim($("#" + inputId2).val());
        var code = $.trim($("#" + inputId).val());
        if (parent != null && parent != '') {
            validDrugUnique(parent, code, inputId);
        } else {
            var regexNumber = /^[A-Z][A-Za-z0-9]$/;
            if (!regexNumber.test(code)) {
                toastr.error('父级类别为空时，类别代码应以大写字母开头，数字或者字母结尾，如【AA,A1...】');
                $("#" + inputId).val("");
                return false;
            } else {
                validDrugUnique(parent, code, inputId);
            }
        }
    }
    /*验证消耗品代码唯一性*/
    function validDrugUnique(parent, code, inputId) {
        $.ajax({
            type: "POST",
            url: "${ctx}/csm/expClassDict/validUnique?parent.id=" + parent + "&classCode=" + code,
            dataType: "json",
            success: function (data) {
                if (data.code == "success") {
                    return true;
                } else if (data.code == 'error') {
                    $("#" + inputId).val('');
                    toastr.error(data.data);
                    return false;
                }
            }, error: function (data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }

</script>
<div class="content-wrap">
    <div class="wrapper" style="bottom: 50px;">
        <ul class="nav nav-tabs">
            <li>
                <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/csm/expClassDict/index')">消耗品类别字典表列表</a>
            </li>
            <li class="active">
                <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/csm/expClassDict/form')">
                    <c:choose>
                        <c:when test="${expClassDict.id==''|| expClassDict.id==null}">
                            消耗品类别字典表添加
                        </c:when>
                        <c:otherwise>消耗品类别字典表修改</c:otherwise>
                    </c:choose>
                </a>
            </li>
        </ul>
        <section class="panel panel-default">
            <div class="panel-body">
                <div class="col-lg-12">
                    <form:form id="inputForm" data-parsley-validate="" modelAttribute="expClassDict"
                               onsubmit="return formSaveLoad('mainCenterDiv','inputForm','${ctx}/csm/expClassDict/save','${ctx}/csm/expClassDict/index');" method="post" class="form-horizontal">
                        <form:hidden path="id" />
                        <div class="form-group">
                            <label class="col-sm-2 control-label">父级类别：</label>

                            <div class="col-sm-4">
                                <sys:treeselect selectedParent="false" id="expClassDict" name="parent.id" value="${expClassDict.parent.id}" update="${not empty expClassDict.id}"
                                                labelName="parent.className" labelValue="${expClassDict.parent.className}"
                                                title="消耗品类别" url="/csm/expClassDict/treeData" extId="${expClassDict.id}" cssClass="form-control" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">类别代码：</label>

                            <div class="col-sm-4">
                                <c:choose>
                                    <c:when test="${expClassDict.id!=null && expClassDict.id!=''}">
                                        <form:input path="classCode" value="${fns:subStringLast(expClassDict.classCode,2 )}" id="classCode" htmlEscape="false" class="form-control" minlength="2"
                                                    maxlength="2" data-parsley-required="true" readonly="true" />
                                    </c:when>
                                    <c:otherwise>
                                        <form:input path="classCode" id="classCode" htmlEscape="false" class="form-control" minlength="2" maxlength="2" data-parsley-required="true"
                                                    onblur="return validRule('classCode','expClassDictId')" />
                                    </c:otherwise>
                                </c:choose>

                            </div>
                            <span class="help-inline">父级类别为空时，类别代码应以大写字母开头，数字或者字母结尾，如【AA,A1...】</span>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">类别名称：</label>

                            <div class="col-sm-4">
                                <form:input path="className" htmlEscape="false" class="form-control" maxlength="40" data-parsley-required="true" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">备注信息：</label>

                            <div class="col-sm-4">
                                <form:textarea path="remarks" htmlEscape="false" class="form-control" maxlength="1000" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存">&nbsp;
                                <input id="btnCancel" class="btn" type="button" value="返 回" onclick="loadDiv('mainCenterDiv','${ctx}/csm/expClassDict/index')">
                            </div>
                        </div>
                    </form:form>
                </div>

            </div>
        </section>
    </div>
</div>